from datetime import datetime
import time

from django.shortcuts import render

# Create your views here.

from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.recruit.models import Enterprise, Recruit, City
from apps.recruit.serializer import CitySerializer, EnterpriseSerializer, RecruitSerializer, Enterprise_Serializer, \
    Recruit_Serializer


#
# class EnterpriseViewSet(ModelViewSet):
#     queryset = Enterprise.objects.all()
#
#     def get_serializer_class(self):
#         if self.action == "retrieve":
#             return EnterpriseSerializer
#         else:
#             return EnterpriseSerializerSimple
#
#     # 按访问量获取最热的企业  enterprise/search/hotlist/
#     @action(methods=["GET"], detail=False, url_path="search/hotlist")
#     def get_hot_enterprise(self, request):
#         hot_enterprises = self.get_queryset().order_by("-visits")[0:4]
#         serializer = self.get_serializer(instance=hot_enterprises, many=True)
#         return Response(serializer.data)
#
#     # 更新公司信息访问量  enterprise/{pk}/visits/
#     @action(methods=['put'], detail=True)
#     def visit(self, request, pk):
#         enterprise = self.get_object()
#         enterprise.visits += 1
#         enterprise.save()
#         return Response({'success': True, 'message': '更新成功'})
#
#     # 收藏企业 enterprise/{pk}/collect/
#     @action(methods=['post'], detail=True)
#     def collect(self, request, pk):
#         try:
#             user = request.user
#             if user and user.is_authenticated:
#                 enterprise = self.get_object()
#                 enterprise.users.add(user)
#                 enterprise.save()
#                 return Response({'success': True, 'message': '收藏成功'})
#         except Exception:
#             return Response({'success': False, 'message': '未登录'}, status=400)
#
#     # 取消收藏企业 enterprise/{pk}/cancelcollect/
#     @action(methods=['post'], detail=True)
#     def recollect(self, request, pk):
#         try:
#             user = request.user
#             if user and user.is_authenticated:
#                 enterprise = self.get_object()
#                 enterprise.users.remove(user)
#                 enterprise.save()
#                 return Response({'success': True, 'message': '取消收藏成功'})
#         except Exception:
#             return Response({'success': False, 'message': '未登录'}, status=400)
#
#
# class RecruitViewSet(ModelViewSet):
#     queryset = Recruit.objects.filter(state="1").order_by("-createtime")
#
#     def get_serializer_class(self):
#         if self.action == 'retrieve':
#             return RecruitSerializer
#         else:
#             return RecruitSerializerSimple
#
#     # 招聘首页获取最新的4个职位 recruits/search/latest/
#     @action(methods=["GET"], detail=False, url_path="search/latest")
#     def get_latest_job(self, request):
#         jobs = self.get_queryset()[0:4]
#         serializer = self.get_serializer(instance=jobs, many=True)
#         return Response(serializer.data)
#
#     # 招聘首页获取推荐的4个职位 recruits/search/recommend/
#     # 目前先简单实现,和最新职位的逻辑一致,后续再调整为推荐
#     @action(methods=["GET"], detail=False, url_path="search/recommend")
#     def get_recommend_job(self, request):
#         jobs = self.get_queryset()[0:4]
#         serializer = self.get_serializer(instance=jobs, many=True)
#         return Response(serializer.data)
#
#     # 根据城市名称和关键字搜索职位 recruits/search/city/keyword/
#     @action(methods=["POST"], detail=False, url_path="search/city/keyword")
#     def search_job(self, request):
#         cityname = request.data.get('cityname')
#         keyword = request.data.get('keyword')
#
#         jobs = self.get_queryset()
#         ret_jobs = []
#         if not cityname and not keyword:
#             ret_jobs = jobs
#         elif cityname and not keyword:
#             for job in jobs:
#                 if job.city == cityname:
#                     ret_jobs.append(job)
#         elif not cityname and keyword:
#             for job in jobs:
#                 if job.jobname.lower().find(keyword.lower()) != -1:
#                     ret_jobs.append(job)
#         else:
#             for job in jobs:
#                 if job.city == cityname and job.jobname.lower().find(keyword.lower()) != -1:
#                     ret_jobs.append(job)
#
#         serializer = self.get_serializer(instance=ret_jobs, many=True)
#         return Response(serializer.data)
#
#     # 更新招聘信息访问量 recruits/{pk}/visit/
#     @action(methods=['put'], detail=True)
#     def visit(self, pk):
#         recruit = self.get_object()
#         recruit.visits += 1
#         recruit.save()
#         return Response({'success': True, 'message': '更新成功'})
#
#     # 收藏招聘信息 recruits/{pk}/collect/
#     @action(methods=['post'], detail=True)
#     def collect(self, request, pk):
#         try:
#             user = request.user
#         except Exception:
#             user = None
#
#         if user is not None and user.is_authenticated:
#             recruit = self.get_object()
#             recruit.users.add(user)
#             recruit.save()
#             return Response({'success': True, 'message': '收藏成功'})
#         else:
#             return Response({'success': False, 'message': '未登录'}, status=400)
#
#             # 收藏招聘信息
#
#     # 收藏招聘信息 recruits/{pk}/collect/
#     @action(methods=['post'], detail=True)
#     def cancelcollect(self, request, pk):
#         try:
#             user = request.user
#         except Exception:
#             user = None
#
#         if user is not None and user.is_authenticated:
#             recruit = self.get_object()
#             recruit.users.remove(user)
#             recruit.save()
#             return Response({'success': True, 'message': '取消收藏成功'})
#         else:
#             return Response({'success': False, 'message': '未登录'}, status=400)
#
#
# class CityViewSet(ModelViewSet):
#     serializer_class = CitySerializer
#     queryset = City.objects.all()
#
#     # 获取热门城市列表  city/hotlist/
#     @action(methods=["GET"], detail=False, url_path="hotlist")
#     def hot_list(self, request):
#         cities = self.get_queryset().filter(ishot=1)
#         serializer = self.get_serializer(instance=cities, many=True)
#         return Response(serializer.data)


class EnterpriseViewSet(ModelViewSet):
    queryset = Enterprise.objects.all()

    def get_serializer_class(self):
        if self.action == "retrieve":
            return EnterpriseSerializer
        else:
            return Enterprise_Serializer

    @action(methods=["PUT"], detail=True)
    def visit(self, request, pk):
        """
        更新企业的访问量
        :param request:
        :param pk:
        :return:
        """
        enterprise = self.get_object()
        enterprise.visits += 1
        enterprise.save()
        return Response({'success': True, 'message': '更新成功'})

    @action(methods=["POST"], detail=True)
    def collect(self, request, pk):
        user = request.user
        if user is not None and user.is_authenticated:
            enterprise = self.get_object()
            enterprise.users.add(user)
            enterprise.save()
            return Response({'success': True, 'message': '收藏成功'})
        else:
            return Response({'success': False, 'message': '未登录'}, status=400)

    @action(methods=["POST"], detail=True)
    def cancelcollect(self, request, pk):
        user = request.user
        if user is not None and user.is_authenticated:
            enterprise = self.get_object()
            enterprise.users.remove(user)
            enterprise.save()
            return Response({'success': True, 'message': '取消收藏成功'})
        else:
            return Response({'success': False, 'message': '未登录'}, status=400)

    @action(methods=["GET"], detail=False, url_path='search/hotlist')
    def get_hotlist(self, request):
        enterprises = self.get_queryset().order_by('-visits')[0:4]
        ser = self.get_serializer(enterprises, many=True)
        return Response(ser.data)


class RecruitViewSet(ModelViewSet):
    queryset = Recruit.objects.filter(state=1)

    def get_serializer_class(self):
        if self.action == 'retrieve':
            return RecruitSerializer
        else:
            return Recruit_Serializer

    @action(methods=["GET"], detail=False, url_path="search/recommend")
    def get_recommend(self, request):
        jobs = self.get_queryset().order_by('-visits')[0:4]
        ser = self.get_serializer(jobs, many=True)
        return Response(ser.data)

    @action(methods=["GET"], detail=False, url_path="search/latest")
    def get_latest(self, request):
        jobs = self.get_queryset().order_by('-createtime')[0:4]
        ser = self.get_serializer(jobs, many=True)
        return Response(ser.data)

    @action(methods=["POST"], detail=False, url_path="search/city/keyword")
    def city_keword(self,request):
        city_name = request.data.get('cityname')
        keyword = request.data.get('keyword')
        jobs = self.get_queryset()
        recruit_jobs = []
        if not city_name and not keyword:
            recruit_jobs = jobs
        elif city_name and not keyword:
            for job in jobs:
                if job.city == city_name:
                    recruit_jobs.append(job)
        else:
            for job in jobs:
                if job.city == city_name and job.jobname.lower().find(keyword.lower())!=-1:
                    recruit_jobs.append(job)
        ser = self.get_serializer(recruit_jobs, many=True)
        return Response(ser.data)

    @action(methods=["PUT"], detail=True)
    def visit(self, request, pk):
        """
        更新职位的访问量
        :param request:
        :param pk:
        :return:
        """
        recruit = self.get_object()
        recruit.visits += 1
        recruit.save()
        return Response({'success': True, 'message': '更新成功'})

    @action(methods=["POST"], detail=True)
    def collect(self, request, pk):
        user = request.user
        if user is not None and user.is_authenticated:
            recruit = self.get_object()
            recruit.users.add(user)
            recruit.save()
            return Response({'success': True, 'message': '收藏成功'})
        else:
            return Response({'success': False, 'message': '未登录'}, status=400)

    @action(methods=["POST"], detail=True)
    def cancelcollect(self, request, pk):
        user = request.user
        if user is not None and user.is_authenticated:
            recruit = self.get_object()
            recruit.users.remove(user)
            recruit.save()
            return Response({'success': True, 'message': '取消收藏成功'})
        else:
            return Response({'success': False, 'message': '未登录'}, status=400)


class CityViewSet(ModelViewSet):
    # 指定查询集
    queryset = City.objects.all()
    # 指定序列化器
    serializer_class = CitySerializer

    @action(methods=["GET"], detail=False, url_path="hotlist")
    def hot_list(self, request):
        cities = self.get_queryset().filter(ishot=1)
        ser = self.get_serializer(cities, many=True)
        return Response(ser.data)
